package cn.gov.cus.rmftquartz.handler;

import cn.gov.cus.rmftquartz.common.utils.ExceptionHelper;
import cn.gov.cus.rmftquartz.common.utils.HGLogHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * SpringBoot中能将所有抛出的异常都拦截住的类。可在此做统一结果处理。
 * （但只是被抛到Controller层的，像定时器类是不行的。而且定时器项目纯后台，没有页面，也没法返回到错误页面，所以也是不行的。）
 * @author wxming
 *
 */
@ControllerAdvice
public class GlobalExceptionHandler {
	private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
	
	@Autowired
	private HGLogHelper hGLogHelper;
	
	@ExceptionHandler(value = Exception.class)
	public ModelAndView exceptionHandler(HttpServletRequest request, Exception ex){
		logger.error(ExceptionHelper.getExMsg(ex));
		hGLogHelper.byIOSystemLog("统一拦截",ex);
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("exception",ex);
		modelAndView.setViewName("error/error");
		return modelAndView;
	}

}
